﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using UtilPublic.ServerLoger;
using ServerBase.DataBaseRedis;
using ServerBase.Protocol;
using ServerBase.Other;
using System.Collections.Generic;
using static UtilPublic.Redis.ServerRedis;
using static ServerGame.Service.Dispatcher;
using UtilPublic.Serialization;
using ServerGame.Service;
using static ServerBase.Config.Conf;
using ServerBase.Service;
using UtilPublic.Mysql;
using static ServerBase.DataBaseRedis.RdsTxt;
using static ServerBase.Service.BaseDispatch;
using ServerBase.Config;
using ServerBase.IniFile;
using ServerPublic.Manager;
using System;
using static ServerBase.IniFile.IniConf;
using ServerGame.GameObject;
using System.Net;
using System.Linq;
using ServerPublic.DataBaseMysql;
using ServerBase.LunarSocket;
using UtilPublic.ApiDispatcher;
using ServerPublic.Data;
using ServerGame.Data;


namespace ServerGame.Manager
{
    public partial class DispatcherMsg
    {
        //是否快速注册
        public static readonly bool 自动创建玩家角色 = EGlobalId.自动创建玩家角色.ValueBool();

        [ApiDpPro((short)EProtocolId.C2G_LOGIN_PLAYERLOGIN, "登陆玩家")]
        public short OnLoginPlayerLogin()
        {
            (var Req, var Rsp) = ThisReq.Body.OnLoginPlayerLogin();

            //设置连接ID类型
            ThisSession.SessionUuid = Req.Uuid;
            ThisSession.SessionType = 0;

            if (PubUserMgr.CheckRestrictLogin(ThisSession.IpAddress()))
            {
                Rsp.Shuttle = PubDataAllMgr.RestrictLogin;
                Rsp.Result = EProtocolResult.登陆受限; return Send(Rsp);
            }
            //if (BaseServerInfo.ServerRunModeIs外网)
            //{
            //    if (Req.Pin != BaseServerInfo.CfgPlatform.VersionPin)
            //    {
            //        Rsp.Result = EProtocolResult.游戏版本不一致; return Send(Rsp); return;
            //    }
            //}
            if (DataGame.DictPlayerOnline.Count > BaseServerInfo.Cfg.RestrictOnline)
            {
                Rsp.Result = EProtocolResult.服务器拥挤; return Send(Rsp);
            }
            if (Req.Code != Util.SimpleVerifyCode(Req.Uuid))
            {
                Rsp.Result = EProtocolResult.验证错误请重新登陆; return Send(Rsp);
            }

            //用户在不同服务器的Puid User:Puid:<Uuid> -- ConfigId -- Puid
            Rsp.Puid = PubUserMgr.GetUuidPuid(Req.Uuid, BaseServerInfo.ConfigId);
            ThisSession.Puid = Rsp.Puid;

            if (PubPlayerMgr.CheckPlayer(Rsp.Puid))
            {
                Rsp.Result = LoginMgr.PlayerLogin(ThisSession);
            }
            else
            {
#if PRESSURE_TEST //压力测试
                Rsp.Result = PlayerCreate(ThisSession, true);
                Rsp.Shuttle = "new";
#else
                if (自动创建玩家角色)
                {
                    Rsp.Result = LoginMgr.PlayerCreate(ThisSession, true);
                    Rsp.Shuttle = "new";
                }
                else
                {
                    //Rsp.Result = EProtocolResult.无玩家数据请创建角色;
                    Rsp.Shuttle = "create";
                }
#endif
            }
            Rsp.Puid = ThisSession.Puid;
            Send(Rsp);

            if (Rsp.Success && Rsp.Shuttle != "create")
            {
                var player = (Player)ThisSession.SessionObj;

                player.Message("欢迎来到游戏。");
                player.RoomEnterLogin();
            }
            return 0;
        }
        [ApiDpPro((short)EProtocolId.C2G_LOGIN_CHECKNAME, "检测名字")]
        public short OnLoginCheckName()
        {
            var player = (Player)ThisSession.SessionObj;
            (var Req, var Rsp) = ThisReq.Body.OnLoginCheckName();

            Rsp.Result = LoginMgr.CheckPlayerName(Req.Name);

            return Send(Rsp);
        }

        [ApiDpPro((short)EProtocolId.C2G_LOGIN_PLAYERCREATE, "玩家创建")]
        public short OnLoginPlayerCreate()
        {
            (var Req, var Rsp) = ThisReq.Body.OnLoginPlayerCreate();

            //loger.Debug($"OnLoginPlayerCreate {Req.Name}");
            Rsp.Puid = ThisSession.Puid;
            Rsp.Result = LoginMgr.PlayerCreate(ThisSession, false, Req.Name, Req.Gender, Req.Family);

            Send(Rsp);

            if (Rsp.Success)
            {
                var player = (Player)ThisSession.SessionObj;

                player.Message("角色创建成功。");
                player.Message("欢迎来到游戏。");
                player.RoomEnterLogin();
            }
            return 0;
        }
        [ApiDpPro((short)EProtocolId.C2G_LOGIN_PUIDLOGIN, "管理员使用PUID直接登录")]
        public short OnLoginPuidLogin()
        {
            (var Req, var Rsp) = ThisReq.Body.OnLoginPuidLogin();

            ThisSession.Puid = Req.Puid;
            Rsp.Puid = ThisSession.Puid;
            if (Req.Pin != Util.SimpleVerifyCode(Req.Puid))
            {
                Rsp.Result = EProtocolResult.验证错误请重新登陆; return Send(Rsp);
            }

            if (PubPlayerMgr.CheckPlayer(Rsp.Puid))
            {
                Rsp.Result = LoginMgr.PlayerLogin(ThisSession);
            }
            else
            {
                Rsp.Result = EProtocolResult.玩家不存在; return Send(Rsp);
            }

            return Send(Rsp);
        }
    }
}